package com.example.bailin.giftsay.sort.activity.SearchFragment;

import android.widget.ListView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.entity.SearchGLEntity;
import com.example.bailin.giftsay.main.UpDataEvent;
import com.example.bailin.giftsay.sort.adapter.SearchGLAdapter;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by bailin on 16/8/26.
 */
public class GLFragment extends BaseFragment {

    private ListView lv;
    private SearchGLAdapter adapter;

    public GLFragment() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected int setLayout() {
        return R.layout.fragment_selection;
    }

    @Override
    protected void initView() {
        lv = bindView(R.id.lv_selection);
    }

    @Override
    protected void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getType(UpDataEvent upDataEvent) {
        if (upDataEvent.getType() == 1) {
        adapter.setIndex(upDataEvent.getPosition());
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(String upDataEvent) {

        tool().getData(UrlProvide.SEARCH_GL + upDataEvent + UrlProvide.SEARCH_GL1, SearchGLEntity.class, new NetTool.NetInterface<SearchGLEntity>() {
            @Override
            public void onSuccess(SearchGLEntity searchGLEntity) {
                adapter = new SearchGLAdapter();
                adapter.setSearchGLEntity(searchGLEntity);
                lv.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}